<?php
final class field {

    public function __construct() {
        
    }

    /**
     * 获得表字段
     * @param type $mid
     * @return type 
     */
    public function get_all($mid) {
        $field = include "./data/field/field_" . $mid . ".php";
        if(empty($field)){
        	$field = array();
        }
        foreach ($field as $k => $v) {
            $fieldSet = array();
            eval("\$fieldSet=" . $v['fieldset'] . ';');
            $field[$k]['fieldset'] = $fieldSet;
            //$field[$k]['html'] = self::field_html($v);
        }
        if(empty($field)){
        	$field = array();
        }
        return $field;
    }

    /**
     * 得到HTML中的表单
     * @param array $v
     * @return type 
     */
    public function field_html($field) {
    	$fieldset = $field['fieldset'];
        $fieldName = strstr($field['tablename'], "_data") ?
                "bigbody[" . $field['fname'] . "]" : $field['fname'];  //字段名
        $class = $fieldset['css'];  //css类名
        $validate = $field['validate']; //验证规则 
        $value = isset($fieldset['value'])?$fieldset['value']:''; //默认值
        //upload
        $file_limit = isset($fieldset['limit'])?$fieldset['limit']:'';//文件上传的数量限制
        $file_text = isset($fieldset['text'])?$fieldset['text']:''; //上传按钮显示的文字
        $file_width = isset($fieldset['width'])?$fieldset['width']:''; //预览图宽度
        $file_height = isset($fieldset['height'])?$fieldset['height']:''; //预览图高度
        $file_imgsize = isset($fieldset['imgsize'])?$fieldset['imgsize']:''; //生成图像大小  如果设置值 为：200,200,300,300，则生成2张图片，第1张图为200*200第2张图为300*300
        $file_type = isset($fieldset['type'])?$fieldset['type']:''; //允许上传的类型
        $file_size = isset($fieldset['size'])?$fieldset['size']:''; //允许上传的大小
        
        //编辑器
        $edi_height = isset($fieldset['height'])?$fieldset['height']:''; //编辑器高度
        $edi_max = isset($fieldset['max'])?$fieldset['max']:''; //编辑器最多字数
        $edi_style = isset($fieldset['style'])?$fieldset['style']:''; //编辑器最多字数
        //单选框 
        $box_list = isset($fieldset['list'])?$fieldset['list']:''; //用户填写的list
        $box_check = isset($fieldset['value'])?$fieldset['value']:'';//用户填入的默认值
        switch ($field['type']) {
            case "text": //单行文本
                $type = (int) $fieldset['ispassword'] ? "password" : "text";
                return "<input validate ='$validate' type='$type' name='$fieldName'
                value='$value'  class='$class'/>";
                break;
            case 'textarea': //文本区域
            	return "<textarea class='$class' name='$fieldName' validata ='$validate'
            	 style='width:300px;height:60px;'>$value</textarea>";
            	break;
            case 'upload': //文件上传
				$arr = array(
				'name'=>$fieldName,
				'maxsize'=>$file_size,
				);
				return hdBaseTag::_kindupload($arr,'');
            	break;
            case 'editor':
            	$arr =  array(
            	'name'=>$fieldName,
            	'height'=>$edi_height,
            	'style'=>$edi_style,
            	);
            	return hdBaseTag::_editor($arr,'');
            	break;
            case 'int':
            	return "<input validate ='$validate' type='$type' name='$fieldName'
                value='$value'  class='$class'/>";
            	break;
            case 'radio':
            	$box_list_arr = strToArr($box_list); //重新组合的list
            	$box_check_arr = explode(',', $box_check);
            	$str = '';
            	foreach($box_list_arr as $k=>$v){
            		if(in_array($k, $box_check_arr)){
            			$str .= "<input validate='$validate' type='radio' name='$fieldName' value='$k' checked='checked'/>$v";
            		}else{
            			$str .= "<input validate='$validate' type='radio' name='$fieldName' value='$k'/>$v";
            		}
            	}
            	return $str;
            	break;
            case 'checkbox';
            	$box_list_arr = strToArr($box_list); //重新组合的list
            	$box_check_arr = explode(',', $box_check);
            	$str = '';
            	$str .= "<input validate='$validate' type='hidden' name='{$fieldName}[]' value='' checked='checked'/>";
            	foreach($box_list_arr as $k=>$v){
            		if(in_array($k, $box_check_arr)){
            			$str .= "<input validate='$validate' type='checkbox' name='{$fieldName}[]' value='$k' checked='checked'/>$v";
            		}else{
            			$str .= "<input validate='$validate' type='checkbox' name='{$fieldName}[]' value='$k'/>$v";
            		}
            	}
            	return $str;
            	break;
            default:
            	break;
        }
    }

}
?>
